//
//  HLEViewController.m
//  HLENetwork
//
//  Created by 13434436448@163.com on 09/28/2020.
//  Copyright (c) 2020 13434436448@163.com. All rights reserved.
//

#import "HLEViewController.h"
#import "HLERequestConfig.h"
#import "HLENetwork.h"
#import <AFNetworking/AFNetworking.h>

@interface HLEViewController ()
{
    HLEBaseRequest *_request;
}

@end

@implementation HLEViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_request cancelRequest];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    HLESessionManager *manager = [HLESessionManager initWithConfig:[HLERequestConfig new]];
    [manager enableLog:YES];
    _request = [manager url:@"https://www.baidu.com" method:HLERequestMethodGET parameters:nil completion:^(HLEBaseResponse * _Nonnull response) {
        if (response.success) {
            //请求返回200，处理自己的逻辑业务
            id responseObject = response.responseObject; //请求返回结构体
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response.response;
            NSDictionary *allHeaderFields = httpResponse.allHeaderFields;//返回的请求头
        }else{
            ///请求不成功
            NSError *error = response.error;//不成功的错误信息
        }
    }];
    
}

- (void)testDemo{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"https://www.baidu.com" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"AFHTTPSessionManager success:%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"AFHTTPSessionManager failure:%@",error);
    }];
}

- (void)testDemo1{
   HLESessionManager *manager = [HLESessionManager defaultManager];
   [manager url:@"https://www.baidu.com" method:HLERequestMethodGET parameters:nil completion:^(HLEBaseResponse * _Nonnull response) {
        NSLog(@"HLESessionManager response:%@",response);
    }];
}

- (void)testDemo2{
    NSURLSession *session = [NSURLSession  sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"NSURLSession data:%@",data);
    }];
    [task resume];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc{
    NSLog(@"HLEViewController dealloc:%@",self);
}
@end
